import reqwest from "reqwest";
import {message} from "antd";

module.exports = {
    fetch(url, params, callback) {
        let api;
        params.access_token = localStorage.token;
        api = this.getUrl(); 
        reqwest({
            url: api + url,
            method: 'POST',
            data: params,
            type: 'json',
            success: (result) => {
                if (result.result == "1") {
                    if(result.code=="ZG0000"){
                     // location= "/";
                    }
                    message.error(result.message, 3);
                    //location = "/";
                } else {
                    callback(result);
                }
            },
            error: (err) => {
                let res = JSON.parse(err.response);
                callback({
                    result: '1',
                    message: res.message
                });
            }
        });
    },
    getUrl() {
        return 'http://10.0.52.92:8380';
    },
    getToken() {
        return localStorage.token;
    },
    setLocalStorage(token) {
        localStorage.token = token;
    },
    setDelaymonth(delaymonth) {
        localStorage.delaymonth = delaymonth;
    },
    getDelaymonth() {
        return localStorage.delaymonth;
    },
    setFunds(funds) {
        localStorage.funds = funds;
    },
    getFunds() {
        return JSON.parse(localStorage.funds);
    },
    logout(cb) {
        delete localStorage.token;
        if (cb)
            cb()
    },
    loggedIn() {
        return !!localStorage.token
    },
    getQueryDate(day) {
        let myDate = new Date();
        let month = myDate.getMonth();
        if (this.getDelaymonth() == undefined) {
            this.setDelaymonth(-1);
        }
        let delaymonth = Number(this.getDelaymonth());
        if (delaymonth == 0) {
            myDate.setDate(myDate.getDate() + day);
        } else {
            myDate.setMonth(month + delaymonth);
            myDate.setDate(1);
            myDate.setDate(myDate.getDate() + day);
        }
        month = myDate.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        let d = myDate.getDate();
        if (d < 10) {
            d = "0" + d;
        }
        return myDate.getFullYear() + "-" + month + "-" + d;
    },
    numberformat(num, len) {
        if (num == undefined) {
            return "";
        }
        num = num.toString();
        let l = num.indexOf('.');

        if (l != -1) {
            l += 1;
            let to = num.substring(len, num.length);
            while (to.length < l) {
                num += '0';
                to += '0';
            }
            num = num.substring(0, l + len);
        } else {
            num += '.';
            l = num.length;
            while (num.length < l + len) {
                num += '0';
            }
        }
        return num;
    },
    FormatDate(date) {
        let month = date.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        let day = date.getDate();
        if (day < 10) {
            day = "0" + day;
        }
        return date.getFullYear() + "-" + month + "-" + day;
    },
    currentDate(day) {
        let myDate = new Date();
        if (day != undefined) {
            myDate.setDate(myDate.getDate() + day);
        }
        let month = myDate.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        let day1 = myDate.getDate();
        if (day1 < 10) {
            day1 = "0" + day1;
        }
        return myDate.getFullYear() + "-" + month + "-" + day1;
    },
    currentDate_month(m) {
        let myDate = new Date();
        if (m != undefined) {
            myDate.setMonth(myDate.getMonth() + m);
        }
        let month = myDate.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        let day1 = myDate.getDate();
        if (day1 < 10) {
            day1 = "0" + day1;
        }
        return myDate.getFullYear() + "-" + month + "-" + day1;
    },
    milliFormat(s) { //娣诲姞鍗冧綅绗�
        if (/[^0-9-\.]/.test(s))
            return "invalid value";
        s = s.replace(/^(\d*)$/, "$1.");
        s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
        s = s.replace(".", ",");
        var re = /(\d)(\d{3},)/;
        while (re.test(s)) {
            s = s.replace(re, "$1,$2");
        }
        s = s.replace(/,(\d\d)$/, ".$1");
        return s.replace(/^\./, "0.")
    },
    uuid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] =
            hexDigits.substr((s[19] & 0x3) | 0x8,
                1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        var uuid = s.join("");
        return uuid;
    }
}
